let gulp = require("./node_modules/gulp");
let cssmin = require("./node_modules/gulp-cssmin");
let jsmin = require("./node_modules/gulp-uglify");
let htmlmin = require("./node_modules/gulp-htmlmin");
let gulpclean = require("./node_modules/gulp-clean");
let webservers = require("./node_modules/gulp-webserver");

function css() {
  return gulp.src("./src/css/**").pipe(cssmin()).pipe(gulp.dest("./dist/css"));
}

function js() {
  return gulp.src("./src/js/**").pipe(jsmin()).pipe(gulp.dest("./dist/js"));
}

function img() {
  return gulp.src("./src/img/**").pipe(gulp.dest("./dist/img"));
}
function html() {
  return gulp
    .src("./src/html/**")
    .pipe(
      htmlmin({
        collapseWhitespace: true, // 表示去除空格
        removeEmptyAttributes: true, // 移出空的属性
        minifyCSS: true, // 压缩 style 标签
        minifyJS: true, // 压缩 script 标签
      })
    )
    .pipe(gulp.dest("./dist/html"));
}

function clean() {
  return gulp.src(["./dist"]).pipe(gulpclean());
}

function webserver() {
  return gulp.src("./dist").pipe(
    webservers({
      host: "localhost", // 域名
      port: 3000, // 监听的端口号，统一写 3000
      open: "./html/index.html", // 打开的页面，相对于 dist 文件夹来的目录
      livereload: true, // 浏览器自动刷新,
      proxies: [
        {
          source: "/api", //随便起，相当于你要请求接口的替换名 ajax请求时就用这个名字
          target: "http://guoye.com/xmyp/api", //需要请求的接口
        },
      ],
    })
  );
}

function watchs() {
  gulp.watch("./src/css", css);
  gulp.watch("./src/js", js);
  gulp.watch("./src/img", img);
  gulp.watch("./src/html", html);
}

exports.clean = clean;
exports.css = css;
exports.js = js;
exports.img = img;
exports.html = html;
exports.webserver = webserver;
exports.watchs = watchs;
exports.build = gulp.series(
  gulp.parallel(css, js, img, html),
  webserver,
  watchs
);
exports.cbuild = gulp.series(
  clean,
  gulp.parallel(css, js, img, html),
  webserver,
  watchs
);
